package leetcode.dp;

/**
 * @author mazouri
 * @create 2021-09-17 22:35
 */
public class _53_maxSubArray {
    public int maxSubArray(int[] nums) {
        int max = nums[0];
        int[] dp = new int[nums.length];
        dp[0] = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (dp[i - 1] > 0) dp[i] = dp[i - 1] + nums[i];
            else dp[i] = nums[i];

            max = Math.max(max, dp[i]);
        }

        return max;
    }

    public int maxSubArray1(int[] nums) {
        int max = nums[0];
        int prev = 0;

        for (int num : nums) {
            prev = Math.max(prev+num,num);
            max = Math.max(max, prev);
        }

        return max;
    }
}
